package top.healthylife.docx4jhelper.base;

import io.swagger.annotations.ApiModel;
import lombok.experimental.Accessors;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : GaoZeXi
 * @date 2022/1/18 11:13
 */

@Accessors(chain = true)
@ApiModel("模板对象父类")
public class Docx4jBaseTpl {
    /**
     * 表格数据
     */
    protected Map<Class<?>, List<?>> tableDataMap;

    /**
     * @param tableData
     * @param <T>
     * @return
     */
    public <T> Docx4jBaseTpl setTableDataMap(List<T> tableData) {
        if (tableDataMap == null) {
            tableDataMap = new HashMap<>(4);
        }
        Class<T> genericType = getGenericType(tableData);
        if (genericType != null) {

            tableDataMap.put(genericType, tableData);
        }
        return this;
    }

    private static <T> Class<T> getGenericType(List<T> tableData) {
        if (tableData == null) {
            return null;
        }

        return (Class<T>) tableData.get(0).getClass();
    }

    public Map<Class<?>, List<?>> getTableDataMap() {
        return this.tableDataMap;
    }
}
